<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        #box {
            position: absolute;
            left: 0;
            top: 0;
            width: 100px;
            height: 100px;
            background-color: red;
        }

        #box2 {
            position: absolute;
            left: 0;
            top: 220px;
            width: 100px;
            height: 100px;
            background-color: skyblue;
        }

        #target {
            width: 1px;
            position: absolute;
            left: 500px;
            top:-200px;
            height: 500px;
            background-color: black;
        }
    </style>
</head>

<body>
    <div id="box"></div>
    <div id="box2"></div>
    <div id="target"></div>
    <div style="margin-top:120px;">
        <button>开始</button><button>停</button>
    </div>

    <div id="second" style="position:absolute;top:350px;">
        <button>开始运动</button>
    </div>
    <script>
        var oBox = document.querySelector('#box');
        var step = 3;// 步长
        var startBtn = document.querySelectorAll('button')[0];
        var endBtn = document.querySelectorAll('button')[1];
        startBtn.onclick = function () {
            clearInterval(oBox.timer);// 1. 清除上一次的定时器，避免定时器累加
            oBox.timer = setInterval(() => {
                // 1. 获取原来的left值
                var oldLeft = parseFloat(getComputedStyle(oBox, null)['left']);
                // 2. 累加一个新的left值
                var newLeft = oldLeft + step;
                if (newLeft > 500) {
                    clearInterval(oBox.timer);// 1. 停止定时器
                    newLeft = 500;
                }
                // 3. 赋值给元素的left属性
                oBox.style.left = newLeft + 'px';
            }, 1000 / 60);
        }
        endBtn.onclick = function () {
            clearInterval(oBox.timer);
        }



        var twoBtn = document.querySelector('#second button');
        var oBox2 = document.querySelector('#box2');
       // var timer2 = null;// 将定时器放在运动物体的私有属性上
        twoBtn.onclick = function(){
            clearInterval(oBox2.timer);
            oBox2.timer = setInterval(()=>{
                // 1. 获取原来的left值
                var oldLeft = parseFloat(getComputedStyle(oBox2, null)['left']);
                // 2. 累加一个新的left值
                var newLeft = oldLeft + step;
                if (newLeft > 500) {
                    clearInterval(oBox2.timer);// 1. 停止定时器
                    newLeft = 500;
                }
                // 3. 赋值给元素的left属性
                oBox2.style.left = newLeft + 'px';
            },1000/60)
        }

    </script>
</body>

</html>